﻿using Inaction.Database.Schema;
using Inaction.Rule;
using System;
using System.Data;
using System.Data.Common;

namespace Inaction.Database
{
    [Serializable]
    public class DbProvider : Inaction.DbProvider, IDisposable
    {
        #region 属性
        string paraFix = string.Empty;
        public override string ParaFix
        {
            get { return paraFix; }
        }
        int maxParaCount = -1;

        internal override int MaxParaCount
        {
            get { return maxParaCount; }
        }

        [NonSerialized]
        System.Data.Common.DbProviderFactory factory = null;

        [NonSerialized]
        private SchemaProviderBase schemaProvider;

        public SchemaProviderBase SchemaProvider
        {
            get { return schemaProvider; }
        }

        SqlType sqlType = SqlType.SqlServer;
        public override SqlType SqlType
        {
            get { return sqlType; }
        }
        public string ConnectionString
        {
            get;
            private set;
        }
        #endregion

        static DbProvider()
        {
            Inaction.DbProvider.initProviderAction = () => { new DbConnecteForm().ShowDialog(); };
        }
        public DbProvider()
            : this(AppConfig.Instance.SqlType, AppConfig.Instance.ConnectionString)
        {
        }
        public DbProvider(SqlType sqlType, string connectionString)
        {
            try
            {
                this.sqlType = sqlType;
                this.ConnectionString = connectionString;
                factory = DbProviderManager.GetFactory(SqlType);
                schemaProvider = DbSchemaProvider.GetSchemaProvider(sqlType, connectionString);
                Tables = schemaProvider.Tables;
                Views = schemaProvider.Views;
                NameFormater = schemaProvider.NameFormater;
                paraFix = schemaProvider.ParaFix;
                maxParaCount = schemaProvider.MaxParaCount;
                connected = true;
            }
            catch (Exception)
            {
                this.ConnectionString = string.Empty;
                throw;
            }
        }
        public void Refresh()
        {
            try
            {
                factory = DbProviderManager.GetFactory(SqlType);
                schemaProvider = DbSchemaProvider.GetSchemaProvider(sqlType, this.ConnectionString);
                Tables = schemaProvider.Tables;
                Views = schemaProvider.Views;
                paraFix = schemaProvider.ParaFix;
                maxParaCount = schemaProvider.MaxParaCount;
            }
            catch (Exception)
            {
                initProviderAction = () => { new DbConnecteForm().ShowDialog(); };
                throw;
            }
        }

        public override void ParseException(Exception ex)
        {
            schemaProvider.ParseException(ex);
        }

        protected internal override string GetPageSql(Type type, int pageSize, int pageIndex)
        {
            return schemaProvider.GetPageSql(type, pageSize, pageIndex);
        }

        internal override string ParseBatchSql(string sql)
        {
            return schemaProvider.ParseBatchSql(sql);
        }

        protected override IDbConnection CreateConnection()
        {
            var con = factory.CreateConnection();
            con.ConnectionString = ConnectionString;
            return con;
        }

        public void Dispose()
        {

        }
    }
}
